﻿using GalaSoft.MvvmLight;
using WpfClient.Model;
using System.Text;
using System.Windows.Input;

namespace WpfClient.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        /// <summary>
        /// The <see cref="WelcomeTitle" /> property's name.
        /// </summary>
        public const string WelcomeTitlePropertyName = "WelcomeTitle";
        public const string FeedPropertyName = "Feed";
        public const string ItemsPropertyName = "ItemTitle";
        public const string ItemsFeedName = "Item";
        public const string CurrentItem = "CurrentItem";
        public const string CurrentFeedPropertyName = "CurrentFeed";
        public const string HtmlContentPropertyName = "HtmlContent";
        public const string LoginVisibilityPropertyName = "LoginVisibility";
        public const string ContentVisibilityPropertyName = "ContentVisibility";
        public const string LoginCommandPropertyName = "LoginCommand";

        private EP.RssFeedItem _currentItem = new EP.RssFeedItem();
        private string _welcomeTitle = string.Empty;
        private EP.RssFeed[] _feed = new EP.RssFeed[] { };
        private EP.RssFeed _currentFeed = new EP.RssFeed();
        private EP.RssFeedItem[] _item = new EP.RssFeedItem[] {};
        private string _htmlContent = string.Empty;
        private string _loginVisibility = string.Empty;
        private string _contentVisibility = string.Empty;


        public ICommand loginCommand { get; private set; }

        public string loginVisibility
        {
            get { return _loginVisibility; }
            set
            {
                if (_loginVisibility == value)
                    return;
                _loginVisibility = value; 
                RaisePropertyChanged(LoginVisibilityPropertyName);
                MessengerInstance.Send<string>(loginVisibility, "loginVisibility");
            }
        }

        public string contentVisibility
        {
            get { return _contentVisibility; }
            set
            {
                if (_contentVisibility == value)
                    return;
                _contentVisibility = value; 
                RaisePropertyChanged(ContentVisibilityPropertyName);
                MessengerInstance.Send<string>(contentVisibility, "contentVisibility");
            }
        }

        public EP.RssFeedItem currentItem
        {
            get { return _currentItem; }
            set 
            {
                if (_currentItem == value)
                    return;

                _currentItem = value; 
                RaisePropertyChanged(CurrentItem);
                MessengerInstance.Send<EP.RssFeedItem>(currentItem, "currentItemChanged");
            }
        }

        public EP.RssFeed[] feed
        {
            get
            {
                return _feed;
            }

            set
            {
                if (_feed == value)
                    return;
                
                _feed = value;
                RaisePropertyChanged(FeedPropertyName);
            }
        }

        public EP.RssFeed currentFeed
        {
            get
            {
                return _currentFeed;
            }

            set
            {
                if (_currentFeed == value)
                    return;
                _currentFeed = value;
                RaisePropertyChanged(CurrentFeedPropertyName);
                MessengerInstance.Send<EP.RssFeed>(currentFeed, "currentFeedChanged");
            }
        }

        public EP.RssFeedItem[] item
        {
            get
            {
                return _item;
            }

            set
            {
                if (_item == value)
                    return;
                _item = value;
                RaisePropertyChanged(ItemsFeedName);

            }
        }

        public string htmlContent
        {
            get { return _htmlContent; }
            set 
            { 
                if (_htmlContent == value) 
                    return;

                byte[] array = Encoding.UTF8.GetBytes(value);
                array = UTF8Encoding.Convert(Encoding.Default, Encoding.UTF8, array);
                _htmlContent = UTF8Encoding.UTF8.GetString(array);
                RaisePropertyChanged(HtmlContentPropertyName);
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            

            _dataService.GetFeedList(
                (feedlist, error) =>
                {
                    if (error != null)
                        return;
                    feed = feedlist.Feeds;
                });
            ;

            MessengerInstance.Register<EP.RssFeed>(this, "currentFeedChanged", message =>
                {
                    _dataService.GetFeedItemList(
                    (liste, error) =>
                    {
                        
                        if (error != null)
                            return;
                        item = liste.FeedsItem;
                        
                    }, message.id);
                });

            MessengerInstance.Register<EP.RssFeedItem>(this, "currentItemChanged", message =>
            {
                htmlContent = message.description;
                _dataService.ReadItem(message.id);
            });

            MessengerInstance.Register<string>(this, "contentVisibility", message =>
            {
                contentVisibility = "Visible";
            });

            MessengerInstance.Register<string>(this, "loginVisibility", message =>
            {
                loginVisibility = "Hidden";
            });    
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}